package com.bootdo.task.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.task.domain.TTaskDO;
import com.bootdo.task.service.TTaskService;
import com.bootdo.task.util.PackageTaskType;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.provider.service.ProviderInfoService;
import com.bootdo.staff.domain.StaffUserDO;
import com.bootdo.staff.service.StaffUserService;

/**
 * 任务表
 * 
 * @author jsn
 * @email 1992lcg@163.com
 * @date 2018-04-26 13:25:18
 */
 
@Controller
@RequestMapping("/task/tTask")
public class TTaskController {
	@Autowired
	private TTaskService tTaskService;
	@Autowired 
	private PackageTaskType packageTaskType;
	@GetMapping()
	@RequiresPermissions("task:tTask:tTask")
	String TTask(){
	    return "task/tTask/tTask";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("task:tTask:tTask")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TTaskDO> tTaskList = tTaskService.list(query);
		for (TTaskDO tTaskDO : tTaskList) {
			tTaskDO = packageTaskType.packageTTaskPojo(tTaskDO);
		}
		int total = tTaskService.count(query);
		PageUtils pageUtils = new PageUtils(tTaskList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "task/tTask/add";
	}

	@GetMapping("/edit/{taskId}")
	@RequiresPermissions("task:tTask:edit")
	String edit(@PathVariable("taskId") Integer taskId,Model model){
		TTaskDO tTask = tTaskService.get(taskId);
		model.addAttribute("tTask", tTask);
	    return "task/tTask/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( TTaskDO tTask){
		if(tTaskService.save(tTask)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( TTaskDO tTask){
		tTaskService.update(tTask);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer taskId){
		if(tTaskService.remove(taskId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] taskIds){
		tTaskService.batchRemove(taskIds);
		return R.ok();
	}
	
}
